/*
   Copyright (C) 2006
   Andrew Madigan

This file is part of nhttpd.

nhttpd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

nhttpd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with nhttpd; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are notn
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

#include <nhttpd/detect.h>
#include <stdio.h>
#include <setjmp.h>
#include <stdarg.h>
#include <syslog.h>
#include <nhttpd/list.h>
#include <nhttpd/error.h>
#include <nhttpd/log.h>

#if NH_THREADS <= 0
List* errorStack;
List* jumpStack;
int consumed_jumps;
#endif

#if NH_THREADS >= 0
#include <pthread.h>
pthread_key_t error_stack_key;
pthread_key_t jump_stack_key;
pthread_key_t consumed_jumps_key;
#endif

#if NH_THREADS < 0
#define nh_st_try nh_try
#define nh__st_throw nh__throw
#define nh_st_error nh_error
#define nh_st_end_try nh_end_try
#elif NH_THREADS > 0
#define nh_thr_try nh_try
#define nh__thr_throw nh__throw
#define nh_thr_error nh_error
#define nh_thr_end_try nh_end_try
#endif

void NH_Error_Free(void* _self) {
	NH_Error* self = _self;
	free(self->msg);
	free(self);
}

#if NH_THREADS <= 0
bool nh_st_try() {
	jmp_buf* env = malloc(sizeof(jmp_buf));
	if (!setjmp(*env))	{
		L_Add(jumpStack, env);
		return true;
	}
	else {
		L_Remove(jumpStack, jumpStack->tail);
		consumed_jumps++;
		return false;
	}
}

void nh__st_throw(int code, char* format, ...) {
	va_list args;
	va_start(args, format);
	NH_log(LOG_CRIT, format, args);
	va_end(args);
	NH_Error* error = malloc(sizeof(NH_Error));
	error->code = code;
	L_Add(errorStack, error);
	longjmp(jumpStack->tail->val, 1);
}



NH_Error* nh_st_error() {
	return errorStack->tail->val;
}

void nh_st_end_try() {
	if (consumed_jumps) {
		--consumed_jumps;
		L_Remove(errorStack, errorStack->tail);
	}
	else L_Remove(jumpStack, jumpStack->tail);
}

#endif
#if NH_THREADS >= 0

bool nh_thr_try() {
	List* jumpStack = pthread_getspecific(jump_stack_key);
	if (jumpStack == NULL) {
		jumpStack = L_Create();
		jumpStack->freeValues = true;
		pthread_setspecific(jump_stack_key, jumpStack);
	}
	
	int* consumedJumps = pthread_getspecific(consumed_jumps_key);
	
	if (consumedJumps == NULL) {
		consumedJumps = malloc(sizeof(int));
		*consumedJumps = 0;
		pthread_setspecific(consumed_jumps_key, consumedJumps);
	}
	
	jmp_buf* env = malloc(sizeof(jmp_buf));
	if (!setjmp(*env)) {
		L_Add(jumpStack, env);
		return true;
	}
	else {
		L_Remove(jumpStack, jumpStack->tail);
		(*consumedJumps)++;
		return false;
	}
}

void nh__thr_throw(int code, char* format, ...) {
	List* jumpStack = pthread_getspecific(jump_stack_key);
	List* errorStack = pthread_getspecific(error_stack_key);
	
	if (errorStack == NULL) {
		errorStack = L_Create();
		errorStack->freeValues = true;
		errorStack->Free = NH_Error_Free;
		pthread_setspecific(error_stack_key, errorStack);
	}
	
	va_list args;
	va_start(args, format);
	NH_log(LOG_CRIT, format, args);
	va_end(args);
	NH_Error* error = malloc(sizeof(NH_Error));
	error->code = code;
	L_Add(errorStack, error);
	longjmp(jumpStack->tail->val, 1);
}

NH_Error* nh_thr_error() {
	List* errorStack = pthread_getspecific(error_stack_key);
	return errorStack->tail->val;
}

void nh_thr_end_try() {
	List* jumpStack = pthread_getspecific(jump_stack_key);
	List* errorStack = pthread_getspecific(error_stack_key);
	int* consumedJumps = pthread_getspecific(consumed_jumps_key);
	
	if (*consumedJumps) {
		--(*consumedJumps);
		L_Remove(errorStack, errorStack->tail);
	}
	else L_Remove(jumpStack, jumpStack->tail);
}
#endif

void NHE_init() {
#if NH_THREADS == 0
if (NHD_THREADS) {
	nh_try = nh_thr_try;
	nh_end_try = nh_thr_end_try;
	nh__throw = nh__thr_throw;
	nh_error = nh_thr_error;
#endif
#if NH_THREADS >= 0
	pthread_key_create(&jump_stack_key, NULL);
	pthread_key_create(&error_stack_key, NULL);
	pthread_key_create(&consumed_jumps_key, NULL);
#endif
#if NH_THREADS == 0
}
else {
	nh_try = nh_st_try;
	nh_end_try = nh_st_end_try;
	nh__throw = nh__st_throw;
	nh_error = nh_st_error;
#endif
#if NH_THREADS <= 0
errorStack = L_Create();
errorStack->freeValues = true;
errorStack->Free = NH_Error_Free;
jumpStack = L_Create();
jumpStack->freeValues = true;
consumed_jumps = 0;
#endif
#if NH_THREADS == 0
}
#endif
}
